<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2019/5/11
 * Time: 9:12
 */
namespace app\admin\model;
use app\admin\controller\NewBuilder;
use app\zhongxue\model\Teachers;
use think\Db;
use think\Exception;
use tp5er\Backup;

class Subject
{
    protected $cid = 0;
    protected $model = [];
    protected $column = [];
    protected $model_id = 0;
    protected $field = [];
    protected $data = [];
    protected $xia = [];

    /**设置栏目id
     * @param null $cid
     * @return $this
     */
    public function setCid($cid=null,$xia=false,$child=false,$limit=null,$sort=true){
        if($cid){
            $this->cid = $cid;
            $column = Column::where('id',$cid)->findOrEmpty()->toArray();
            if($column){
                if($xia){
                    $this->setXia($this->getXiaJi($column['id'],0,$child,$limit));
                }
                $this->setColumn($column);
                if(isset($column['model_id'])&&$column['model_id']){
                    $this->setModelId($column['model_id'],$sort);
                }
            }
        }
        return $this;
    }

    /**获取
     * @param int $pid
     * @param int $chengji
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getXiaJi($pid=0,$chengji=0,$child=false,$limit=null){
        $data = Db::name(session('modular').'_'.$this->getDbName())
            ->where('pid',$pid)
            ->where('delete_time',0)
            ->select();
        if(!$data){
            return [];
        }
        foreach ($data as $key=>$value){
            $data[$key]['xia'] = $this->getXiaJi($value['id'],$chengji+1);
            $data[$key]['cheng'] = $chengji;
            if($child){
                $model = Models::where('id',$value['model_id'])->findOrEmpty()->toArray();
                if($model){
                    $shuju = Db::name($model['model_table'])
                        ->where('delete_time',0)
                        ->order('sort desc,id asc');
                    if($limit){
                        $shuju = $shuju->paginate($limit)->toArray();
                    }else{
                        $shuju = $shuju->select();
                    }
                    $data[$key]['child'] = $shuju;
                }

            }
        }
        return $data;
    }
    /**获取栏目id
     * @return int
     * @throws Exception
     */
    public function getCid(){
        if(isset($this->cid)&&$this->cid){
            return $this->cid;
        }else{
            return null;
        }
    }

    /**设置栏目
     * @param null $column
     * @return $this
     */
    public function setColumn($column=null){
        if($column){
            $this->column = $column;
        }
        return $this;
    }

    /**获取栏目
     * @return array
     * @throws Exception
     */
    public function getColumn(){
        if(isset($this->column)&&$this->column){
            return $this->column;
        }else{
            return [];
        }
    }

    /**设置模型id
     * @param null $id
     * @return $this
     */
    public function setModelId($id=null,$sort=true){
        if($id){
            $this->model_id = $id;
            $model = Models::where('id',$id)->findOrEmpty()->toArray();
            if($model){
                $this->setModel($model);
                if($sort){
                    $field = Fields::where('model_id',$id)->order('sort desc')->select()->toArray();
                }else{
                    $field = Fields::where('model_id',$id)->order('id asc')->select()->toArray();
                }

//                $field = Db::name('field')->where('model_id',$model['id'])->order('sort desc')->select();
                $this->setField($field);
            }
        }
        return $this;
    }

    /**获取模型id
     * @return int
     * @throws Exception
     */
    public function getModelId(){
        return $this->model_id;
    }

    /**设置模型
     * @param null $model
     * @return $this
     */
    public function setModel($model=null){
        if($model){
            $this->model = $model;
        }
        return $this;
    }

    /**设置下级信息
     * @param null $array
     * @return $this
     */
    public function setXia($array=null){
        if($array){
            $this->xia = $array;
        }
        return $this;
    }

    /**获取下级信息
     * @return array
     */
    public function getXia(){
        return $this->xia;
    }

    /**获取模型
     * @return array
     * @throws Exception
     */
    public function getModel(){
        return $this->model;
    }

    /**设置字段
     * @param null $field
     * @return $this
     */
    public function setField($field=null){
        if($field){
            $this->field = $field;
        }
        return $this;
    }

    /**获取字段
     * @return array
     * @throws Exception
     */
    public function getField(){
        return $this->field;
    }

    /**设置字段的信息
     * @param array $data
     * @return array
     */
    public function setData($data=[]){
        $jiu = null;
        $document = [];
        if($data){
            $this->data = $data;
            if(isset($data['id'])&&$data['id']){
                $jiu = Db::name($this->model['model_table'])->where('id',$data['id'])->find();
                $document['id'] = $data['id'];
            }
            foreach ($this->getField() as $value){
                //更新时间
                if($value['fields']=='update_time'){
                    $document['update_time'] = time();
                    continue;
                }
                //文本添加
                if($value['form_type']=='单行文本'||$value['form_type']=='多行文本'||$value['form_type']=='下拉框'||$value['form_type']=='单选框'||$value['form_type']=='编辑框'){
                    if(isset($data[$value['fields']])){
                        $document[$value['fields']] = $data[$value['fields']];
                    }
                }
                if($value['form_type']=='多选框'){
                    if(isset($data[$value['fields']])){
                        $document[$value['fields']] = implode(',',$data[$value['fields']]);
                    }else{
                        $document[$value['fields']] = '';
                    }
                }
                if($value['form_type']=='开关'){
                    if(isset($data[$value['fields']])&&$data[$value['fields']]=='on'){
                        $arr = 1;
                    }else{
                        $arr = 0;
                    }
                    $document[$value['fields']] = $arr;
                }
                if($value['form_type']=='时间'){
                    $document[$value['fields']] = strtotime($data[$value['fields']]);
                }
                if($value['form_type']=='单文件'){
                    if(isset($data[$value['fields']])&&$data[$value['fields']]){
                        $document[$value['fields']] = $data[$value['fields']];
                        if(isset($jiu[$value['fields']])&&$jiu[$value['fields']]){
                            $this->changeDelFileId($jiu[$value['fields']]);
                        }
                    }
                }
                if($value['form_type']=='单图片'){
                    if(isset($data[$value['fields']])){
                        $document[$value['fields']] = $data[$value['fields']];
                        if(isset($jiu[$value['fields']])&&$jiu[$value['fields']]){
                            $this->changeDelFileId($jiu[$value['fields']]);
                        }
                    }
                }

                if($value['form_type']=='多文件'){
                    if(isset($data[$value['fields']])&&$data[$value['fields']]){
                        $document[$value['fields']] = trim($data[$value['fields']],',');
                        if(isset($jiu[$value['fields']])&&$jiu[$value['fields']]){
                            $arrays = explode(',',$jiu[$value['fields']]);
                            foreach ($arrays as $v){
                                $this->changeDelFileId($v);
                            }
                        }
                    }
                }
                if($value['form_type']=='多图片'){
                    if(isset($data[$value['fields']])){
                        $document[$value['fields']] = trim($data[$value['fields']],',');
                        if(isset($jiu[$value['fields']])&&$jiu[$value['fields']]){
                            $arrays = explode(',',$jiu[$value['fields']]);
                            foreach ($arrays as $v){
                                $this->changeDelFileId($v);
                            }
                        }
                    }
                }
                if($value['form_type']=='联动下拉框'){
                    if(isset($data[$value['fields']])){
                        $document[$value['fields']] = $data[$value['fields']];
                    }
                }
            }
            $this->data = $document;
        }else{
            $this->data = [];
        }
        return $this;
    }

    /**获取字段信息
     * @return array
     */
    public function getData(){
        return $this->data;
    }

    /**删除附件
     * @param $id
     * @return int|string
     */
    public function changeDelFileId($id){
        return Enclosure::where('id','in',$id)->update([
           'status'=>0
        ]);
    }

    /**检查数据是否可删除
     * @param array $array
     * @param bool $true
     * @param null $table
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function inspectDelete($array=[],$true=false,$table=null){
        if($true){
            if(isset($array['delete'])&&$array['delete']==1){
                return true;
            }else{
                return false;
            }
        }
        if($table){
            $shuju = Db::name($table)->where('id','in',$array)->field('id,delete')->select();
            foreach ($shuju as $value){
                if(isset($value['delete'])&&$value['delete']==0){
                    return false;
                }
            }
            return true;
        }
        foreach ($array as $value){
            if(isset($value['delete'])&&$value['delete']==0){
                return false;
            }
        }
        return true;
    }

    /**添加权限
     * @param array $array
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    static public function addLianBiaoJur($array=[]){
        $role = Roles::where('id',1)->json(['role'])->findOrEmpty()->toArray();
        if(isset($array['model_id'])&&$array['model_id']){
            $model_id = $array['model_id'];
            $model = Models::where('id',$model_id)->findOrEmpty()->toArray();
            if($model){
                if($model['model_type']=="联表模型"){
                    if($model){
                        $str = ['增','删','改','查'];
                        foreach ($str as $value){
                            $bool = Jurs::create([
                                'jur_name'=>$model['model_name'],
                                'model_id'=>$model_id,
                                'descs'=>$value,
                                'status'=>1,
                                'create_time'=>time()
                            ]);
                            $role['role'][] = (int)$bool->id;
                        }
                    }
                }
            }
        }elseif (isset($array['cid'])&&$array['cid']){
            $str = ['增','删','改','查'];
            $column = Column::where('id',$array['cid'])->findOrEmpty()->toArray();
            if($column){
                foreach ($str as $value){
                    $bool = Jurs::create([
                        'jur_name'=>$column['name'],
                        'cid'=>$array['cid'],
                        'descs'=>$value,
                        'status'=>1,
                        'create_time'=>time()
                    ]);
                    $role['role'][] = (int)$bool->id;
                }
            }
        }
        Roles::where('id',1)->json(['role'])->update($role);
    }

    /**获取递归的栏目
     * @param int $pid
     * @param int $cheng
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function Datas($pid=0,$cheng=0){
        $data = Column::where('pid',$pid)
            ->order('id asc')
            ->select()
            ->toArray();
        if(!$data){
            return [];
        }
        $array = [];
        foreach ($data as $value){
            $value['cheng'] = $cheng;
            $array[] = $value;
            $shujus   = $this->Datas($value['id'],$cheng+1);
            $array = array_merge($array,$shujus);
        }
        return $array;
    }

    /**加载单表
     * @param string $type
     * @param null $cid
     * @param null $id
     * @param bool $edit
     * @return mixed
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getDanBiaoForm($type='add',$cid=null,$id=null,$edit=true){
        if($cid){
            $this->setCid($cid);
        }
        $data = null;
        if($id){
            $model = $this->getModel();
            $data = Db::name($model['model_table'])
                ->where('id',$id)
                ->where('delete_time',null)
                ->find();
        }
        $builder =  NewBuilder::make('form');
        if($type=='add'){
            $builder = $builder->addAction('document/add');
        }else{
            $builder = $builder->addAction('document/edit');
        }
        $builder = $builder->addNone('cid',$cid);
        if($id){
            $builder = $builder->addNone('id',$id);
        }
     // echo '<pre>';
     // print_r($this->getField());exit;
        foreach ($this->getField() as $value){
            if($value['field']=='cid'){
                continue;
            }
            if($value['field']=='update_time'){
                continue;
            }
            if($value['form_type']=='单行文本'){
                if($data){
                    $builder = $builder->addInput($value['field'],$value['field_name'],$data[$value['field']]);
                }else{
                    $builder = $builder->addInput($value['field'],$value['field_name'],$value['defaults']);
                }
            }
            if($value['form_type']=='多行文本'){
                if($data){
                    $builder = $builder->addTextarea($value['field'],$value['field_name'],$data[$value['field']]);
                }else{
                    $builder = $builder->addTextarea($value['field'],$value['field_name'],$value['defaults']);
                }
            }
            if($value['form_type']=='单文件'){
                if(!$edit){
                    $builder = $builder->addInput($value['field'],$value['field_name'],$data[$value['field']]);
                    continue;
                }
                if($data){
                    $builder = $builder->addFile($value['field'],$value['field_name'],$data[$value['field']]);
                }else{
                    $builder = $builder->addFile($value['field'],$value['field_name']);
                }
            }
            if($value['form_type']=='多文件'){
                if(!$edit){
                    $builder = $builder->addInput($value['field'],$value['field_name'],$data[$value['field']]);
                    continue;
                }
                if($data){
                    $builder = $builder->addFiles($value['field'],$value['field_name'],$data[$value['field']]);
                }else{
                    $builder = $builder->addFiles($value['field'],$value['field_name']);
                }
            }
            if($value['form_type']=='单图片'){
                if($data){
                    $builder = $builder->addImg($value['field'],$value['field_name'],$data[$value['field']]);
                }else{
                    $builder = $builder->addImg($value['field'],$value['field_name']);
                }
            }
            if($value['form_type']=='多图片'){
                if($data){
                    $builder = $builder->addImgs($value['field'],$value['field_name'],$data[$value['field']]);
                }else{
                    $builder = $builder->addImgs($value['field'],$value['field_name']);
                }
            }
            if($value['form_type']=='下拉框'){
                $option = explode(',',trim($value['option'],','));
                if($data){
                    $builder = $builder->addSelect($value['field'],$value['field_name'],$data[$value['field']],$option);
                }else{
                    $builder = $builder->addSelect($value['field'],$value['field_name'],$value['defaults'],$option);
                }
            }
            if($value['form_type']=='单选框'){
                $option = explode(',',trim($value['option'],','));
                if($data){
                    $builder = $builder->addRadio($value['field'],$value['field_name'],$data[$value['field']],$option);
                }else{
                    $builder = $builder->addRadio($value['field'],$value['field_name'],$value['defaults'],$option);
                }
            }
            if($value['form_type']=='多选框'){
                $option = explode(',',trim($value['option'],','));
                if($data){
                    $da = explode(',',trim($data[$value['field']]));
                    $builder = $builder->addCheckbox($value['field'],$value['field_name'],$da,$option);
                }else{
                    $builder = $builder->addCheckbox($value['field'],$value['field_name'],$value['defaults'],$option);
                }
            }
            if($value['form_type']=='编辑框'){
                if($data){
                    $builder = $builder->addEditor($value['field'],$value['field_name'],$data[$value['field']]);
                }else{
                    $builder = $builder->addEditor($value['field'],$value['field_name'],$value['defaults']);
                }
            }
            if($value['form_type']=='开关'){
                if($data){
                    $builder = $builder->addSwitch($value['field'],$value['field_name'],$data[$value['field']]);
                }else{
                    $builder = $builder->addSwitch($value['field'],$value['field_name'],$value['defaults']);
                }
            }
            if($value['form_type']=='时间'){
                if($data){
                    $builder = $builder->addTime($value['field'],$value['field_name'],$data[$value['field']],null,null,true);
                }else{
                    $builder = $builder->addTime($value['field'],$value['field_name'],$value['defaults'],null,null,true);
                }
            }
            if($value['form_type']=='状态'){
                if($data){
                    $builder = $builder->addSelect($value['field'],$value['field_name'],$data[$value['field']],['不可','可'],true,true);
                }else{
                    $builder = $builder->addSelect($value['field'],$value['field_name'],$value['defaults'],['不可','可'],true,true);
                }
            }
            if($value['form_type']=='多表联动框'){
              
                if($data){
//                    echo $value['table'];exit;
                 // echo '<pre>';
                  //print_r($data[$value['field']]);exit;
                    $builder = $builder->addPulldown($value['field'],$value['field_name'],$data[$value['field']],$value['table'],$value['option'],'document/duobiaoliandong?cid='.$cid,$value['next_table']);
                }else{
                    $builder = $builder->addPulldown($value['field'],$value['field_name'],$value['defaults'],$value['table'],$value['option'],'document/duobiaoliandong?cid='.$cid,$value['next_table']);
                }
            }
            if($value['form_type']=='联动下拉框'){
                if($data){
//                    echo $value['table'];exit;
                    $builder = $builder->addDropDown($value['field'],$value['field_name'],$data[$value['field']],$value['table'],$value['hierarchy'],'document/getlianbiao?cid='.$cid);
                }else{
                    $builder = $builder->addDropDown($value['field'],$value['field_name'],$value['defaults'],$value['table'],$value['hierarchy'],'document/getlianbiao?cid='.$cid);
                }
            }
        }
        if($edit){
            $builder = $builder->addFormButton('submit','提交');
        }
        $builder = $builder->addFormButton('back','返回');
        return $builder->fetch();

    }
    public function newgetDanBiaoForm($type='add',$cid=null,$id=null,$edit=true){
        if($cid){
            $this->setCid($cid);
        }
        $data = null;
        if($id){
            $model = $this->getModel();
            $data = Db::name($model['model_table'])
                ->where('id',$id)
                ->where('delete_time',null)
                ->find();
        }
        $builder =  NewBuilder::make('form');
        if($type=='add'){
            $builder = $builder->addAction('document/add');
        }else{
            $builder = $builder->addAction('document/edit');
        }
        $builder = $builder->addNone('cid',$cid);
        if($id){
            $builder = $builder->addNone('id',$id);
            $teacher = Teachers::where('status',1)
                ->where('campus_id',$data['campus'])
                ->select()->toArray();
            $newteacher = [];
            foreach ($teacher as $value){
                $newteacher[$value['id']]=$value['name'];
            }
        }else{
            $teacher = Teachers::where('status',1)
                ->select()->toArray();
            $newteacher = [];
            foreach ($teacher as $value){
                $newteacher[$value['id']]=$value['name'];
            }
        }
        foreach ($this->getField() as $value){
            if($value['field']=='cid'){
                continue;
            }
            if($value['field']=='teachers'){
                if($data){
//                    echo $data[$value['field']];exit;
                    $builder = $builder->addSelect($value['field'],$value['field_name'],$data[$value['field']],$newteacher,true);
                }else{
                    $builder = $builder->addSelect($value['field'],$value['field_name'],$value['defaults'],$newteacher,true);
                }
                continue;
            }
            if($value['field']=='update_time'){
                continue;
            }
            if($value['form_type']=='单行文本'){
                if($data){
                    $builder = $builder->addInput($value['field'],$value['field_name'],$data[$value['field']]);
                }else{
                    $builder = $builder->addInput($value['field'],$value['field_name'],$value['defaults']);
                }
            }
            if($value['form_type']=='多行文本'){
                if($data){
                    $builder = $builder->addTextarea($value['field'],$value['field_name'],$data[$value['field']]);
                }else{
                    $builder = $builder->addTextarea($value['field'],$value['field_name'],$value['defaults']);
                }
            }
            if($value['form_type']=='单文件'){
                if(!$edit){
                    $builder = $builder->addInput($value['field'],$value['field_name'],$data[$value['field']]);
                    continue;
                }
                if($data){
                    $builder = $builder->addFile($value['field'],$value['field_name'],$data[$value['field']]);
                }else{
                    $builder = $builder->addFile($value['field'],$value['field_name']);
                }
            }
            if($value['form_type']=='多文件'){
                if(!$edit){
                    $builder = $builder->addInput($value['field'],$value['field_name'],$data[$value['field']]);
                    continue;
                }
                if($data){
                    $builder = $builder->addFiles($value['field'],$value['field_name'],$data[$value['field']]);
                }else{
                    $builder = $builder->addFiles($value['field'],$value['field_name']);
                }
            }
            if($value['form_type']=='单图片'){
                if($data){
                    $builder = $builder->addImg($value['field'],$value['field_name'],$data[$value['field']]);
                }else{
                    $builder = $builder->addImg($value['field'],$value['field_name']);
                }
            }
            if($value['form_type']=='多图片'){
                if($data){
                    $builder = $builder->addImgs($value['field'],$value['field_name'],$data[$value['field']]);
                }else{
                    $builder = $builder->addImgs($value['field'],$value['field_name']);
                }
            }
            if($value['form_type']=='下拉框'){
                $option = explode(',',trim($value['option'],','));
                if($data){
                    $builder = $builder->addSelect($value['field'],$value['field_name'],$data[$value['field']],$option);
                }else{
                    $builder = $builder->addSelect($value['field'],$value['field_name'],$value['defaults'],$option);
                }
            }
            if($value['form_type']=='单选框'){
                $option = explode(',',trim($value['option'],','));
                if($data){
                    $builder = $builder->addRadio($value['field'],$value['field_name'],$data[$value['field']],$option);
                }else{
                    $builder = $builder->addRadio($value['field'],$value['field_name'],$value['defaults'],$option);
                }
            }
            if($value['form_type']=='多选框'){
                $option = explode(',',trim($value['option'],','));
                if($data){
                    $da = explode(',',trim($data[$value['field']]));
                    $builder = $builder->addCheckbox($value['field'],$value['field_name'],$da,$option);
                }else{
                    $builder = $builder->addCheckbox($value['field'],$value['field_name'],$value['defaults'],$option);
                }
            }
            if($value['form_type']=='编辑框'){
                if($data){
                    $builder = $builder->addEditor($value['field'],$value['field_name'],$data[$value['field']]);
                }else{
                    $builder = $builder->addEditor($value['field'],$value['field_name'],$value['defaults']);
                }
            }
            if($value['form_type']=='开关'){
                if($data){
                    $builder = $builder->addSwitch($value['field'],$value['field_name'],$data[$value['field']]);
                }else{
                    $builder = $builder->addSwitch($value['field'],$value['field_name'],$value['defaults']);
                }
            }
            if($value['form_type']=='时间'){
                if($data){
                    $builder = $builder->addTime($value['field'],$value['field_name'],$data[$value['field']],null,null,true);
                }else{
                    $builder = $builder->addTime($value['field'],$value['field_name'],$value['defaults'],null,null,true);
                }
            }
            if($value['form_type']=='状态'){
                if($data){
                    $builder = $builder->addSelect($value['field'],$value['field_name'],$data[$value['field']],['不可','可'],true,true);
                }else{
                    $builder = $builder->addSelect($value['field'],$value['field_name'],$value['defaults'],['不可','可'],true,true);
                }
            }
            if($value['form_type']=='联动下拉框'){
                if($data){
//                    echo $value['table'];exit;
                    $builder = $builder->addDropDown($value['field'],$value['field_name'],$data[$value['field']],$value['table'],$value['hierarchy'],'document/getlianbiao');
                }else{
                    $builder = $builder->addDropDown($value['field'],$value['field_name'],$value['defaults'],$value['table'],$value['hierarchy'],'document/getlianbiao');
                }
            }
        }
        if($edit){
            $builder = $builder->addFormButton('submit','提交');
        }
        $builder = $builder->addFormButton('back','返回');
        return $builder->fetch('public/forms');

    }

    /**添加单表数据
     * @param $data
     * @return mixed
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function addShuJu($data){
        $this->setCid($data['cid']);
        $model = $this->getModel();
        $jiu = null;
        if(isset($data['id'])&&$data['id']){
            $jiu = Db::name($model['model_table'])
                ->where('id',$data['id'])
                ->find();
        }
        foreach ($this->field as $value){
            if($value['field']=='update_time'){
                continue;
            }
            //文本添加
            if($value['form_type']=='单行文本'||$value['form_type']=='多行文本'||$value['form_type']=='下拉框'||$value['form_type']=='单选框'||$value['form_type']=='编辑框'){
                if(isset($data[$value['field']])){
                    $data[$value['field']] = $data[$value['field']];
//                    if($value['form_type']=='编辑框'){
//                        UpFile::copydir('./ueditor','../application/'.session('modular').'/view/ueditor/');
//                    }
                }
            }
            if($value['form_type']=='多选框'){
                if(isset($data[$value['field']])){
                    $data[$value['field']] = implode(',',$data[$value['field']]);
                }else{
                    $data[$value['field']] = '';
                }
            }
            if($value['form_type']=='开关'){
                if(isset($data[$value['field']])&&$data[$value['field']]=='on'){
                    $data[$value['field']] = 1;
                }else{
                    $data[$value['field']] = 0;
                }
            }
            if($value['form_type']=='时间'){
                $data[$value['field']] = strtotime($data[$value['field']]);
            }
            if($value['form_type']=='单文件'){
                if(isset($data[$value['field']])&&$data[$value['field']]){
                    $data[$value['field']] = $data[$value['field']];
                    if(isset($jiu[$value['field']])){
                        $this->changeDelFileId($jiu[$value['field']]);
                    }
                }
            }
            if($value['form_type']=='单图片'){
                if(isset($data[$value['field']])){
                    $data[$value['field']] = $data[$value['field']];
                    if(isset($jiu[$value['field']])){
                        $this->changeDelFileId($jiu[$value['field']]);
                    }
                }
            }
            if($value['form_type']=='多文件'){
                if(isset($data[$value['field']])&&$data[$value['field']]){
                    $data[$value['field']] = trim($data[$value['field']],',');
                    if(isset($jiu[$value['field']])){
                        $this->changeDelFileId($jiu[$value['field']]);
                    }
                }
            }
            if($value['form_type']=='多图片'){
                if(isset($data[$value['field']])){
                    $data[$value['field']] = trim($data[$value['field']],',');
                    if(isset($jiu[$value['field']])){
                        $this->changeDelFileId($jiu[$value['field']]);
                    }
                }
            }
            if($value['form_type']=='联动下拉框'){
                if(isset($data[$value['field']])){
                    $data[$value['field']] = $data[$value['field']];
                }
            }
        }
        $data['update_time'] = time();
        return $data;
    }

    /**联表表单设置
     * @param $model_id
     * @param null $id
     * @param string $type
     * @param bool $edit
     * @return mixed
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getLianBiaoForm($model_id,$pid=null,$id=null,$type='add',$edit=true,$action=null){
        if($model_id){
            $this->setModelId($model_id);
        }
        $data = null;
        if($id){
            $data = Db::name($this->getModel()['model_table'])
                ->where('id',$id)
                ->find();
            if(isset($data['pid'])){
                $pid = $data['pid'];
            }
        }
        $builder = NewBuilder::make('form')
            ->addNone('model_id',$model_id);
        if($id){
            $builder = $builder->addNone('id',$id);
        }
        if(!$action){
            if($type=='add'){
                $builder = $builder->addAction('lianbiao/add');
            }else{
                $builder = $builder->addAction('lianbiao/edit');
            }
        }else{
            $builder = $builder->addAction($action);
        }
        foreach ($this->field as $value){
            if($value['field']=='delete_time'){
                continue;
            }
            if($value['field']=='pwd'){
                $builder = $builder->addInput('pwd','密码');
                continue;
            }
            if($value['field']=='pid'){
                $suoyou = Db::name($this->getModel()['model_table'])
                    ->where('delete_time',null)
                    ->select();
                $option = [0=>'最上级'];
                foreach ($suoyou as $value){
                    $option[$value['id']] = $value['name'];
                }
                $builder = $builder->addSelect('pid','上级',$pid,$option,true);
                continue;
            }
            if($value['form_type']=='单行文本'){
                if($data){
                    $builder = $builder->addInput($value['field'],$value['field_name'],$data[$value['field']]);
                }else{
                    $builder = $builder->addInput($value['field'],$value['field_name']);
                }
            }
            if($value['form_type']=='多行文本'){
                if($data){
                    $builder = $builder->addTextarea($value['field'],$value['field_name'],$data[$value['field']]);
                }else{
                    $builder = $builder->addTextarea($value['field'],$value['field_name']);
                }
            }
            if($value['form_type']=='单文件'){
                if($data){
                    $builder = $builder->addFile($value['field'],$value['field_name'],$data[$value['field']]);
                }else{
                    $builder = $builder->addFile($value['field'],$value['field_name']);
                }
            }
            if($value['form_type']=='多文件'){
                if($data){
                    $builder = $builder->addFiles($value['field'],$value['field_name'],$data[$value['field']]);
                }else{
                    $builder = $builder->addFiles($value['field'],$value['field_name']);
                }
            }
            if($value['form_type']=='单图片'){
                if($data){
                    $builder = $builder->addImg($value['field'],$value['field_name'],$data[$value['field']]);
                }else{
                    $builder = $builder->addImg($value['field'],$value['field_name']);
                }
            }
            if($value['form_type']=='多图片'){
                if($data){
                    $builder = $builder->addImgs($value['field'],$value['field_name'],$data[$value['field']]);
                }else{
                    $builder = $builder->addImgs($value['field'],$value['field_name']);
                }
            }
            if($value['form_type']=='下拉框'){
                $option = explode(',',trim($value['option'],','));
                if($data){
                    $builder = $builder->addSelect($value['field'],$value['field_name'],$data[$value['field']],$option);
                }else{
                    $builder = $builder->addSelect($value['field'],$value['field_name'],null,$option);
                }
            }
            if($value['form_type']=='状态'){
                $option = explode(',',trim($value['option'],','));
                if($data){
                    $builder = $builder->addSelect($value['field'],$value['field_name'],$data[$value['field']],['不可','可'],true);
                }else{
                    $builder = $builder->addSelect($value['field'],$value['field_name'],null,['不可','可'],true);
                }
            }
            if($value['form_type']=='单选框'){
                $option = explode(',',trim($value['option'],','));
                if($data){
                    $builder = $builder->addRadio($value['field'],$value['field_name'],$data[$value['field']],$option);
                }else{
                    $builder = $builder->addRadio($value['field'],$value['field_name'],null,$option);
                }
            }
            if($value['form_type']=='多选框'){
                $option = explode(',',trim($value['option'],','));
                if($data){
                    $da = explode(',',trim($data[$value['field']]));
                    $builder = $builder->addCheckbox($value['field'],$value['field_name'],$da,$option);
                }else{
                    $builder = $builder->addCheckbox($value['field'],$value['field_name'],null,$option);
                }
            }
            if($value['form_type']=='编辑框'){
                if($data){
                    $builder = $builder->addEditor($value['field'],$value['field_name'],$data[$value['field']]);
                }else{
                    $builder = $builder->addEditor($value['field'],$value['field_name']);
                }
            }
            if($value['form_type']=='开关'){
                if($data){
                    $builder = $builder->addSwitch($value['field'],$value['field_name'],$data[$value['field']]);
                }else{
                    $builder = $builder->addSwitch($value['field'],$value['field_name']);
                }
            }
            if($value['form_type']=='时间'){
                if($data){
                    $builder = $builder->addTime($value['field'],$value['field_name'],$data[$value['field']],null,null,true);
                }else{
                    $builder = $builder->addTime($value['field'],$value['field_name'],null,null,null,true);
                }
            }
            if($value['form_type']=='联动下拉框'){
                if($data){
                    $builder = $builder->addDropDown($value['field'],$value['field_name'],$data[$value['field']],$value['table'],$value['hierarchy'],'lianbiao/getlianbiao?model_id='.$model_id);
                }else{
                    $builder = $builder->addDropDown($value['field'],$value['field_name'],null,$value['table'],$value['hierarchy'],'lianbiao/getlianbiao?model_id='.$model_id);
                }
            }
            if($value['form_type']=='多表联动框'){
                if($data){
                    $builder = $builder->addPulldown($value['field'],$value['field_name'],$data[$value['field']],$value['table'],$value['option'],'lianbiao/duobiaoliandong?model_id='.$model_id,$value['next_table']);
                }else{
                    $builder = $builder->addPulldown($value['field'],$value['field_name'],null,$value['table'],$value['option'],'lianbiao/duobiaoliandong?model_id='.$model_id,$value['next_table']);
                }
            }
        }
        if($edit){
            $builder = $builder->addFormButton('submit','提交');
        }
        $builder = $builder->addFormButton('back','返回');
        return $builder->fetch();
    }

    /**整理联表数据
     * @param $data
     * @return int|string
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function addLianBiao($data){
        $this->setModelId($data['model_id']);
        unset($data['model_id']);
        $model = $this->getModel();
        if(isset($data['id'])&&$data['id']){
            $jiu = Db::name($model['model_table'])
               ->where('id',$data['id'])
               ->find();
        }
        foreach ($this->field as $value){
            //文本添加
            if($value['form_type']=='单行文本'||$value['form_type']=='多行文本'||$value['form_type']=='下拉框'||$value['form_type']=='单选框'||$value['form_type']=='编辑框'){
                if(isset($data[$value['field']])){
                    $data[$value['field']] = $data[$value['field']];
//                    if($value['form_type']=='编辑框'){
//                        UpFile::copydir('./ueditor','../application/'.session('modular').'/view/ueditor/');
//                    }
                }
            }
            if($value['form_type']=='多选框'){
                if(isset($data[$value['field']])){
                    $data[$value['field']] = implode(',',$data[$value['field']]);
                }else{
                    $data[$value['field']] = '';
                }
            }
            if($value['form_type']=='开关'){
                if(isset($data[$value['field']])&&$data[$value['field']]=='on'){
                    $data[$value['field']] = 1;
                }else{
                    $data[$value['field']] = 0;
                }
            }
            if($value['form_type']=='时间'){
                $data[$value['field']] = strtotime($data[$value['field']]);
            }
            if($value['form_type']=='单文件'){
                if(isset($data[$value['field']])&&$data[$value['field']]){
                    $data[$value['field']] = $data[$value['field']];
                    if(isset($jiu[$value['field']])){
                        $this->changeDelFileId($jiu[$value['field']]);
                    }
                }
            }
            if($value['form_type']=='单图片'){
                if(isset($data[$value['field']])){
                    $data[$value['field']] = $data[$value['field']];
                    if(isset($jiu[$value['field']])){
                        $this->changeDelFileId($jiu[$value['field']]);
                    }
                }
            }
            if($value['form_type']=='多文件'){
                if(isset($data[$value['field']])&&$data[$value['field']]){
                    $data[$value['field']] = trim($data[$value['field']],',');
                    if(isset($jiu[$value['field']])){
                        $this->changeDelFileId($jiu[$value['field']]);
                    }
                }
            }
            if($value['form_type']=='多图片'){
                if(isset($data[$value['field']])){
                    $data[$value['field']] = trim($data[$value['field']],',');
                    if(isset($jiu[$value['field']])){
                        $this->changeDelFileId($jiu[$value['field']]);
                    }
                }
            }
            if($value['form_type']=='联动下拉框'){
                if(isset($data[$value['field']])){
                    $data[$value['field']] = $data[$value['field']];
                }
            }
        }
        if(isset($data['id'])&&$data['id']){
            return Db::name($model['model_table'])->update($data);
        }else{
            return Db::name($model['model_table'])->insert($data);
        }
    }

    /**添加权限
     * @param $id
     * @return bool
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    static public function addJurToRole($id){
        $role = Roles::where('id',1)->json(['role'])->findOrEmpty()->toArray();
        $role['role'][] = (int)$id;
        if(Roles::where('id',1)->json(['role'])->update($role)){
            return true;
        }else{
            return false;
        }
    }

    /**删除数据
     * @param $id
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    static public function delJurToRoleAll($id){
        if(!is_array($id)){
            $id = explode(',',$id);
        }
        $user = Roles::where('id','>',0)->json(['role'])->select()->toArray();
        foreach ($user as $key=>$value){
            $role = [];
            foreach ($value['role'] as $va){
                if(!in_array((string)$va,$id)){
                    $role[] = $va;
                }
            }
            $value['role'] = $role;
            Roles::where('id',$value['id'])->json(['role'])->update([
               'role'=>$value['role']
            ]);
        }
        return true;
    }

    /**删除图片
     * @param $data
     */
    public function delFieldToFile($data){
        foreach ($data as $value){
            foreach ($this->getField() as $va){
                if($va['form_type']=='单图片'||$va['form_type']=='单文件'||$va['form_type']=='多图片'||$va['form_type']=='多文件'){
                    if($value[$va['field']]){
                        UpFile::delFile($value[$va['field']]);
                    }
                }
            }
        }
    }

    /**还原
     * @param array $array
     * @return bool
     */
    static public function huanyuan($array=[]){
        $id = null;
        $sql = null;
        if(isset($array['id'])&&$array['id']){
            $id = $array['id'];
        }
        if(isset($array['sql'])&&$array['sql']){
            $sql = '.'.trim($array['sql'],'.');
        }
        $db = new Backup();
        if($id){
            $data = Db::name(session('modular').'_databases')->where('id',$id)->find();
            $datas = Db::name(session('modular').'_databases')->where('create_time','>=',$data['create_time'])->select();
            $db->setFile(['name'=>'.'.$data['path'],'part'=>1])->import(0);
            foreach ($datas as $value){
                if(is_file('.'.$value['path'])){
                    unlink('.'.$value['path']);
                }
            }
        }elseif($sql){
            $db->setFile(['name'=>'.'.ltrim($sql,'.'),'part'=>1])->import(0);
        }
        return true;
    }

    /**备份
     * @param $data
     * @return bool
     */
    static public function beifen($data){
        $config = [
            'path'=>'./Data/'
        ];
        $db = new Backup($config);
        $files = $db->dataList();
        $zhenze = '/'.config('database.prefix').session('modular').'_/';
        $newfiles = [];
        foreach ($files as $value){
            if(preg_match($zhenze,$value['name'])){
                $newfiles[] = $value;
            }
        }
        $name = session('modular').'_'.time();
        foreach ($files as $value){
            $db->setFile(['name'=>$name,'part'=>1])->backup($value['name'],0);
        }
        $data['path'] = '/Data/'.$name.'-1.sql';
        $data['name'] = $name.'-1.sql';
        $data['create_time'] = time();
        Db::name(session('modular').'_database_system')->where('id',3)->update(['value'=>time()]);
        if(Db::name(session('modular').'_databases')->insert($data)){
            $dirs = '../application/'.session('modular').'/view/Data';
            if(!is_dir($dirs)){
//                chmod($dirs,0777);
                mkdir($dirs,0777,true);
            }
            copy('.'.$data['path'], '../application/'.session('modular').'/view'.$data['path']);
            return true;
        }else{
            return false;
        }
    }

    /**删除栏目是用于删除权限
     * @param $id
     */
    static public function deleteColumnToJur($id){
        $column = Column::where('id','in',$id)->select()->toArray();
        foreach ($column as $value){
            $jur = Jurs::where('cid',$value['id'])->select()->toArray();
            foreach ($jur as $va){
                Subject::delJurToRoleAll($va['id']);
            }
            Jurs::where('cid',$value['id'])->delete();
        }
    }

    /**
     * 添加访问量
     */
    static public function addFangwen(){
        System::where('name|en_name','fangwen')
            ->setInc('value');
    }
}